package com.cdtye.itps.cms.service.datasysn;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cdtye.common.mscore.config.HttpClientCore;
import com.cdtye.common.webcore.enums.HttpMessageEnum;
import com.cdtye.common.webcore.web.AjaxJson;
import com.cdtye.common.webcore.web.BizException;
import com.cdtye.itps.cms.dao.brain.vo.BrainResponseVo;
import com.xxl.job.core.log.XxlJobLogger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Slf4j
@Service
public class BrainService {

    @Value("${brain.ip}")
    private String BRAIN_IP;

    @Autowired
    private HttpClientCore httpClientCore;

    @Async
    public void handleBrain(BrainResponseVo brainResponseVo) {

        String resultJson = "";
        try{
            String data = JSONObject.toJSONString(brainResponseVo);
            resultJson = httpClientCore.httpPost(BRAIN_IP, data);
            AjaxJson result = JSON.parseObject(resultJson, AjaxJson.class);
            if (result == null || result.getStatus() != HttpMessageEnum.SUCCESS.getStatus()) {
                throw new BizException("返回结果异常");
            }
            XxlJobLogger.log(brainResponseVo.getTableName() + "======Http发送数据到大脑成功。数量=" + brainResponseVo.getData().size());
            log.info(brainResponseVo.getTableName() + "======Http发送数据到大脑成功。数量=" + brainResponseVo.getData().size());
        } catch (Exception e) {
                XxlJobLogger.log(brainResponseVo.getTableName() + "======Http发送数据到大脑异。数量=" + brainResponseVo.getData().size());
            log.error(brainResponseVo.getTableName() + "======Http发送数据到大脑异常。数量=" + brainResponseVo.getData().size());
            throw new BizException("Http发送数据到大脑异常：" + e.getMessage() + "，返回结果：" + resultJson, e);
        }
    }

}
